package com.osce.score.config.socketHandler;

import com.osce.common.core.entity.AjaxResult;
import com.osce.score.service.socket.ExPlaceSocketService;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import javax.annotation.Resource;


/**
 * socket连接监听类
 * 考场
 * @author zzxjs
 */
@Component("scoreExPlaceHandler")
public class ScoreExPlaceHandler extends TextWebSocketHandler {

    @Resource private ExPlaceSocketService socketService;

    /**
     * 连接成功时的监听
     *
     * @param session 会话
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession session) {
        socketService.initConnect(session);
    }

    /**
     * 接收信息
     *
     * @param session 会话
     * @param message 信息
     */
    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) {
        String payload = message.getPayload();
        AjaxResult result = socketService.manageOrder(session, payload);
        socketService.sendMessage(session, result);
    }

    /**
     * 断开连接时的监听
     *
     * @param session 会话
     * @param status  连接状态
     */
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) {
        socketService.close(session);
    }
}
